package com.lehecai.core.search.converter;

import com.lehecai.core.lottery.*;
import com.lehecai.core.search.converter.impl.AbstractSearchEntityPropertyConverter;
import com.lehecai.core.util.CoreJSONUtils;
import com.lehecai.core.util.CoreMathUtils;
import net.sf.json.JSONArray;

import java.util.Date;

/**
 * User: sunshow
 * Date: 13-4-12
 * Time: 上午11:10
 */
public class SearchEntityPropertyConverterDelegate {

    public final static ISearchEntityPropertyConverter LongToLong = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return CoreJSONUtils.getLong(jsonArray, index);
        }
    };

    public final static ISearchEntityPropertyConverter IntegerToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }
    };

    public final static ISearchEntityPropertyConverter StringToLong = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Long(CoreJSONUtils.getLong(jsonArray, index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return String.valueOf(property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return Long.valueOf((String)property);
        }
    };

    public final static ISearchEntityPropertyConverter StringToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return String.valueOf(property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return Integer.valueOf((String)property);
        }
    };

    public final static ISearchEntityPropertyConverter DateToLong = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Long(CoreJSONUtils.getLong(jsonArray, index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            Long longValue = (Long)property;
            if (longValue == 0) {
                return null;
            }
            Date date = new Date();
            date.setTime(longValue);
            return date;
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Long(((Date) property).getTime());
        }
    };

    /**
     * 默认两位精度
     */
    public final static ISearchEntityPropertyConverter DoubleToLong = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Long(CoreJSONUtils.getLong(jsonArray, index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return new Double(CoreMathUtils.div((Long)property, 100));
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Long((long)(CoreMathUtils.mul((Double)property, 100)));
        }
    };

    //order---------------start
    public final static ISearchEntityPropertyConverter LotteryTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return LotteryType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((LotteryType)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter PlatformTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return PlatformType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((PlatformType)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter PlanOrderStatusToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return PlanOrderStatus.getItem((Integer)property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((PlanOrderStatus)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter PrizeStatusToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return PrizeStatus.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((PrizeStatus)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter PlanOrderTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return PlanOrderType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((PlanOrderType)property).getValue());
        }
    };
    //order---------------end

    //recharge---------------start
    public final static ISearchEntityPropertyConverter RechargeStatusToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return RechargeStatus.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((RechargeStatus)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter RechargeTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return RechargeType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((RechargeType)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter BankTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return BankType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((BankType)property).getValue());
        }
    };

    public final static ISearchEntityPropertyConverter WalletTypeToInteger = new AbstractSearchEntityPropertyConverter() {
        @Override
        public Object getFromJSON(JSONArray jsonArray, int index) throws Exception {
            return new Integer(jsonArray.getInt(index));
        }

        @Override
        public Object convertFromSearch(Object property) throws Exception {
            return WalletType.getItem((Integer) property);
        }

        @Override
        public Object convertToSearch(Object property) throws Exception {
            return new Integer(((WalletType)property).getValue());
        }
    };
    //recharge---------------end
}
